package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.impl.dao.BreedFactoryDao;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.enums.SeedPurchaseRecordStatus;
import com.jiantong.work.impl.dao.SeedPurchaseRecordDao;
import com.jiantong.work.model.SeedPurchaseRecord;
import com.jiantong.work.service.SeedPurchaseRecordReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by maowenjie on 2017/4/18.
 *
 */
@Slf4j
@Service
@RpcProvider
public class SeedPurchaseRecordReadServiceImpl implements SeedPurchaseRecordReadService {
    private final SeedPurchaseRecordDao seedPurchaseRecordDao;
    private final BreedFactoryDao breedFactoryDao;
    @Autowired
    public SeedPurchaseRecordReadServiceImpl(SeedPurchaseRecordDao seedPurchaseRecordDao, BreedFactoryDao breedFactoryDao) {
        this.seedPurchaseRecordDao = seedPurchaseRecordDao;
        this.breedFactoryDao = breedFactoryDao;
    }

    @Override
    public Response<SeedPurchaseRecord> findById(Long id) {
        try{
            SeedPurchaseRecord seedPurchaseRecord = seedPurchaseRecordDao.findById(id);
            if(seedPurchaseRecord == null){
                log.error("seedPurchaseRecord(id={}) is not found", id);
                return Response.fail("seedPurchaseRecord.not.found");
            }
            return Response.ok(seedPurchaseRecord);
        }catch (Exception e){
            log.error("failed to find seedPurchaseRecord(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("seedPurchaseRecord.find.fail");
        }
    }

    @Override
    public Response<Map<String, Object>> findStatusById(Long id) {
        try{
            Integer status = seedPurchaseRecordDao.findStatusById(id);
            Map<String , Object> map = Maps.newHashMap();
            if(status == null){
                log.error("seedPurchaseRecord status (id={}) is not found", id);
                return Response.fail("seedPurchaseRecord.status.not.found");
            }
            map.put("status",status);
            return Response.ok(map);
        }catch (Exception e){
            log.error("seedPurchaseRecord status (id={}) is not found", id);
            return Response.fail("seedPurchaseRecord.status.not.found");
        }
    }

    @Override
    public Response<Paging<SeedPurchaseRecord>> pagingSeedPurchaseRecord(Integer pageNo, Integer pageSize,Long breedFactoryId, Integer status) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            if(status != null){
                criteria.put("status",status);
            }
            criteria.put("breedFactoryId",breedFactoryId);
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(seedPurchaseRecordDao.paging(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        }catch (Exception e){
            log.error("failed to paging seedPurchaseRecord cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("seedPurchaseRecord.paging.fail");
        }
    }

    @Override
    public Response<Paging<SeedPurchaseRecord>> pagingSeedPurchaseRecordGqv(Integer pageNo, Integer pageSize, String factoryName, Date fromDate, Date toDate) {
        return null;
    }

    @Override
    public Response<Paging<SeedPurchaseRecord>> pagingSeedPurchaseRecordGqv(Integer pageNo, Integer pageSize, Long breedFactoryId,Integer breedType, Date fromDate, Date toDate) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();

            List<Long> breedFactoryIds = new ArrayList<>();
            if(breedFactoryId != null){
                breedFactoryIds.add(breedFactoryId);
            }else {
                LoginUser loginUser = UserUtil.getCurrentUser();
                breedFactoryIds = loginUser.getBreedFactoryIds();
            }
            //在这里根据breedType赛选一下养殖场,从breedFactoryIds中去掉不是这个养殖类型的养殖场ID
            if (breedType != null) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("breedType",breedType);
                if(!breedFactoryIds.isEmpty()) {
                    map.put("breedFactoryIds", breedFactoryIds);
                    breedFactoryIds = breedFactoryDao.selectedByType(map);
                }
            }

            if(!breedFactoryIds.isEmpty()) criteria.put("breedFactoryIds", breedFactoryIds);
            else return Response.ok(new Paging<>(0L,new ArrayList<>()));

            if(fromDate!=null && toDate!=null){
                criteria.put("fromDate",fromDate);
                criteria.put("toDate",toDate);
            }
            criteria.put("status", SeedPurchaseRecordStatus.FINISHED.value());
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(seedPurchaseRecordDao.pagingSeedPurchaseRecordGqv(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find SeedPurchaseRecord fromDate={},fromDate={},toDate={}),cause:{}",breedFactoryId, fromDate,toDate,Throwables.getStackTraceAsString(e));
            return Response.fail("SeedPurchaseRecord.find.fail");
        }
    }

    @Override
    public Response<Double> sumTotalPriceForWeb(Map<String, Object> map) {
        try{
            return Response.ok(seedPurchaseRecordDao.sumTotalPriceForWeb(map));
        }catch (Exception e){
            log.error("failed to sum total price for web , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to sum total price for web");
        }
    }

    @Override
    public Response<List<Map<String, Object>>> findSeedPurchaseInfoWeb(Map<String, Object> map) {
        try{
            return Response.ok(seedPurchaseRecordDao.findSeedPurchaseInfoWeb(map));
        }catch (Exception e){
            log.error("failed to find seed purchase info web , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find seed purchase info web");
        }
    }

    @Override
    public Response<Paging<SeedPurchaseRecord>> pagingSeedPurchaseRecordGqv(Integer pageNo, Integer pageSize, Map map) {

        try{
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            return Response.ok(seedPurchaseRecordDao.pagingSeedPurchaseRecordGqv(pageInfo.getOffset(), pageInfo.getLimit(), map));
        } catch (Exception e) {
            log.error("failed to find SeedPurchaseRecord fromDate={},fromDate={},toDate={}),cause:{}",map,Throwables.getStackTraceAsString(e));
            return Response.fail("SeedPurchaseRecord.find.fail");
        }

    }
}
